package com.renjibo.teawithmilkgoods.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.renjibo.teawithmilkgoods.Model.GoodsBean;
import com.renjibo.teawithmilkgoods.Model.ProductBean;
import com.renjibo.teawithmilkgoods.R;
import com.renjibo.teawithmilkgoods.adapters.GoodsDetailsAdapter;
import com.renjibo.teawithmilkgoods.adapters.HomeRecycleAdapter;
import com.renjibo.teawithmilkgoods.adapters.HomeRecycleRightAdapter;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


public class GoodsDetailsFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private TextView topic_fragment_textView;
    private RecyclerView topic_fragment_recycleView;
    private Gson gson;
    private ProductBean productBean;

    public GoodsDetailsFragment() {
    }

    public static GoodsDetailsFragment newInstance(String param1, String param2) {
        GoodsDetailsFragment fragment = new GoodsDetailsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        gson = new Gson();
        postHttpJson();
    }
    //请求网络数据
    public void postHttpJson(){
        RequestParams params = new RequestParams(mParam2);
        //params.addQueryStringParameter("wd", "xUtils");
        x.http().post(params, new Callback.CommonCallback<String>(){

            @Override
            public void onSuccess(String result) {
                topic_fragment_textView.setVisibility(View.GONE);
                Log.d("zzz","result:"+result);
                productBean = gson.fromJson(result, ProductBean.class);
                //goodsBean.getData().getCategories().get()
                Log.d("zzz","Goods:"+ productBean.toString());

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(),"服务器繁忙",Toast.LENGTH_SHORT).show();
                Log.d("zzz","onError");
                topic_fragment_textView.setVisibility(View.VISIBLE);
                topic_fragment_textView.setText("服务器繁忙!");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("zzz","onCancelled");
            }

            @Override
            public void onFinished() {
//                if
//                GoodsDetailsAdapter homeRecycleAdapter=new GoodsDetailsAdapter(goodsBean,getActivity());
//                home_recycleView.setAdapter(homeRecycleAdapter);
//                homeRecycleAdapter.setReFreach(HomeFragment.this);
//                home_recycleView_right.setAdapter(new HomeRecycleRightAdapter(goodsBean.getData().getCategories().get(0),getActivity()));
            GoodsDetailsAdapter goodsDetailsAdapter=new GoodsDetailsAdapter(productBean,getActivity());
                topic_fragment_recycleView.setAdapter(goodsDetailsAdapter);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_details, container, false);
        topic_fragment_textView = (TextView) view.findViewById(R.id.topic_fragment_textView);
        topic_fragment_recycleView = (RecyclerView) view.findViewById(R.id.topic_fragment_recycleView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        topic_fragment_recycleView.setLayoutManager(linearLayoutManager);
        return view;
    }

}
